﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassForWinForm
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //初始化一个打开文件对话框
            OpenFileDialog dialog = new OpenFileDialog();
            //对话框标题
            dialog.Title = "选择需要打开的TXT文件";
            //是否允许选择多个文件
            dialog.Multiselect = false;
            //获取或设置当前文件名过滤字符串
            dialog.Filter = "txt files (*.txt)|*.txt";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                textBox_Path.Text = dialog.FileName;
                using (StreamReader sr = new StreamReader(dialog.FileName,Encoding.Default))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        textBox_Data.Text = textBox_Data.Text + line + "\r\n";
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string data = textBox_Data.Text;
            File.WriteAllText(textBox_Path.Text, data,Encoding.Default);
        }
    }
}
